$(document).ready(function()
{	
	//获取参数
	var form=$('#Form_Login');
	var button=$('#Submit_Login');
	
	var username=$('#Username_Login');
	var password=$('#Password_Login');
	var captcha=$('#Captcha_Login');
	var captchaImage=$('#CaptchaImage');
	var captchaLink=captchaImage.attr('src');
	
	var ac=$('#AlertContainer');	
	
	//设置焦点
	username.focus();	
	
	//页面设置
	var location=window.location.toString();
	var tmp=location.split('&');	
	for(var iParam=0;iParam<tmp.length;iParam++)
	{		
		var paramName;
		try{		
			paramName=tmp[iParam].split('=')[0];
		}
		catch(e){}
		if(paramName=='number')
		{			
			var paramValue=tmp[iParam].split('=')[1];
			switch(paramValue)
			{
				case 'PasswordWrong':				
					ac.text('用户名或密码错误');
					ac.fadeIn();
				break;
				
				case 'Locked':
					ac.text('用户己被锁定');
					ac.fadeIn();
				break;		
				
				case 'Inexistence':
					ac.text('用户名或密码错误');
					ac.fadeIn();
				break;
				
				case 'EmailUnverified':
					ac.text('电子邮件未验证');
					ac.fadeIn();
				break;
				
				case 'Logined':
					window.location='/exec.php?action=mc_welcome';
				break;					
				
				case 'Normal':
					window.location='/exec.php?action=mc_welcome';
				break;				
			}
			break;
		}
	}	
	
	//事件绑定-验证码图片
	captchaImage.click(function()
	{
		captchaImage.attr('src',captchaLink+'&'+Math.random());
	});
	
	//事件绑定-看不清刷新
	$('#RefreshCaptcha').click(function()
	{
		captchaImage.attr('src',captchaLink+'&'+Math.random());
	});	
	
	//事件绑定-表单
	form.submit(function()
	{
		if(username.val()=='')
		{
			ac.text('请填写用户名');
			ac.fadeIn();
			username.focus();
			return false;
		}
		
		if(password.val()=='')
		{
			ac.text('请填写密码');
			ac.fadeIn();
			password.focus();			
			return false;
		}		
		
		if(captcha.val()=='')
		{
			ac.text('请填写验证码');
			ac.fadeIn();
			captcha.focus();			
			return false;
		}				
		
		//通过客户端验证，开始服务端验证
		button.attr('disabled',true);
		ac.text('正在登录请稍候...');
		
		//回调
		$.post(form.attr('action'),{Username_Login:username.val(),Password_Login:password.val(),Captcha_Login:captcha.val()},function($data)
		{            
			switch($data)
			{								
				case 'CaptchaError':
					ac.text('验证证码错误');
					ac.fadeIn();
					button.attr('disabled',false);
				break;
				
				case 'PasswordWrong':
					ac.text('用户名或密码错误');
					captchaImage.attr('src',captchaLink+'&'+Math.random());
					ac.fadeIn();
					button.attr('disabled',false);
				break;				
				
				case 'Locked':
					ac.text('用户己被锁定');
					captchaImage.attr('src',captchaLink+'&'+Math.random());
					ac.fadeIn();
					button.attr('disabled',false);
				break;		
				
				case 'Inexistence':
					ac.text('用户名或密码错误');
					captchaImage.attr('src',captchaLink+'&'+Math.random());
					ac.fadeIn();
					button.attr('disabled',false);
				break;
				
				case 'EmailUnverified':
					ac.text('电子邮件未验证');
					captchaImage.attr('src',captchaLink+'&'+Math.random());
					ac.fadeIn();
					button.attr('disabled',false);
				break;
				
				case 'Logined':
					window.location='/exec.php?action=mc_welcome';
				break;					
				
				case 'Normal':
					window.location='/exec.php?action=mc_welcome';
				break;				
				
				default:
					//ac.text('登录失败，请稍候再试。');
					//button.attr('disabled',false);
					document.write($data);
			}
		});		
		return false;
	});
});